//
//  YTaddCollectAddrController.m
//  yitaicaijing
//
//  Created by DIANGHONG on 2018/9/12.
//  Copyright © 2018年 DIANGHONG. All rights reserved.
//

#import "YTaddCollectAddrController.h"
@interface YTaddCollectAddrController ()
@property (weak, nonatomic) IBOutlet UITextField *nameTxtField; // 名字
@property (weak, nonatomic) IBOutlet UITextField *moblieTxtField;   // 手机号
@property (weak, nonatomic) IBOutlet UILabel *cityLabel;    // 省、市、区
@property (weak, nonatomic) IBOutlet UITextView *detailAddrTxtView; // 详细地址

@property (weak, nonatomic) IBOutlet UIButton *addOrEditeButton;
@property(nonatomic ,strong)NSArray *pcrList; // 省、市、区数组
@end

@implementation YTaddCollectAddrController

- (instancetype)init
{
    self = [super init];
    if (self) {
        UIStoryboard *sb = [UIStoryboard storyboardWithName:@"YTaddCollectAddrController" bundle:nil];
        YTaddCollectAddrController *vc = [sb instantiateViewControllerWithIdentifier:@"addCollectAddrID"];
        self = vc;
        
    }
    return self;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.detailAddrTxtView.layer.cornerRadius = 5;
    self.detailAddrTxtView.layer.masksToBounds = YES;
    self.detailAddrTxtView.layer.borderWidth = 1;
    self.detailAddrTxtView.layer.borderColor = [UIColor.lightGrayColor CGColor];
    

    [self.addOrEditeButton setTitle:self.collect_id ? @"更新" : @"添加" forState:UIControlStateNormal];
    [self setupNav];
    
    if (self.collect_id) {
        [self loadData];
    }
}


- (void)loadData
{
    [YTNetworkManager.shareManager requestCollectAddrInfoWithID:self.collect_id
                                                     completion:^(id json, BOOL isSucc, NSString *error)
    {
        if (!isSucc) {
            [self.tableView showErrorWithTipStr:error];
            return ;
        }
        
        self.nameTxtField.text = json[@"collect_name"];
        self.moblieTxtField.text = json[@"phone"];
        self.cityLabel.text = [NSString stringWithFormat:@"%@ %@ %@",json[@"province"],json[@"city"],json[@"county"]];
        self.detailAddrTxtView.text = json[@"addr"];
        self.pcrList = @[json[@"province"],json[@"city"],json[@"county"]];
    }];
}

- (void)setupNav
{
    [self.navigationController.navigationBar setTintColor:UIColor.blackColor];
   
    UILabel *titleLabel = [[UILabel alloc] init];
    titleLabel.text = @"收货地址";
    titleLabel.font = [UIFont boldSystemFontOfSize:16];
    titleLabel.textColor = UIColor.blackColor;
    self.navigationItem.titleView = titleLabel;
    
}

#pragma mark: - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.tableView endEditing:YES];
    // 省、市、区
    if (indexPath.row == 2) {
        [[MOFSPickerManager shareManger] showMOFSAddressPickerWithTitle:nil
                                                            cancelTitle:@"取消"
                                                            commitTitle:@"完成"
                                                            commitBlock:^(NSString *address, NSString *zipcode)
        {
            self.cityLabel.text = [address stringByReplacingOccurrencesOfString:@"-" withString:@" "];
            self.pcrList = [address componentsSeparatedByString:@"-"];
        } cancelBlock:^{
            
        }];
    }
}

#pragma mark: - 添加按钮 监听
- (IBAction)addAddrBtnClick:(UIButton *)sender
{
    if (!self.nameTxtField.hasText ||
        !self.moblieTxtField.hasText ||
        !self.detailAddrTxtView.hasText ||
        (self.cityLabel.text.length == 0))
    {
        [self.tableView showErrorWithTipStr:@"请完善地址信息"];
        return;
    }
    sender.enabled = false;
    [YTNetworkManager.shareManager addOrEditeCollectAddrWithCollect_id:self.collect_id
                                                          collect_name:self.nameTxtField.text
                                                                 phone:self.moblieTxtField.text
                                                              province:self.pcrList[0]
                                                                  city:self.pcrList[1]
                                                                county:self.pcrList[2]
                                                                  addr:self.detailAddrTxtView.text
                                                            completion:^(BOOL isSucc, NSString *error)
    {
        sender.enabled = YES;
        if (!isSucc) {
            [self.tableView showErrorWithTipStr:error];
            return ;
        }
        
        NSString *status = self.collect_id ? @"更改成功" : @"添加成功";
        [self.tableView showTipWithTipStr:status];
        [self.navigationController popViewControllerAnimated:true];
    }];
}


@end
